package core.proto;


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Rectangle;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import static app.constants.CollabVizConstants.*;


public class Annotation {
	private int startIndex; 
	private int endIndex;
	private String annotatedText; 
	public final String docKey;

	private JTextArea textArea;
	private JInternalFrame frame;

	public Annotation(String line) {
		this(line.split(ANNOTATION_DELIMITER));
	}

	public void updateAnnotation(int startIndex, int endIndex, String annotatedText){
		this.startIndex = startIndex;
		this.endIndex = endIndex;
		this.annotatedText = annotatedText;
	}
	
	public int getStartIndex(){
		return startIndex;
	}
	
	public int getEndIndex(){
		return endIndex;
	}
	
	private Annotation(String[] tokens){
		this(tokens[0],
				Integer.parseInt(tokens[1]),
				Integer.parseInt(tokens[2]),
				new Point(Integer.parseInt(tokens[3]), Integer.parseInt(tokens[4])),
				tokens[5],
				tokens[6]
				);
	}

	public String toSerializeString(){
		final String format = "%s%s%d%s%d%s%d%s%d%s%s%s%s";
		String line = String.format(format,
				docKey, ANNOTATION_DELIMITER,
				this.startIndex, ANNOTATION_DELIMITER,
				this.endIndex, ANNOTATION_DELIMITER,
				(int) this.getFrame().getLocation().getX(), ANNOTATION_DELIMITER,
				(int) this.getFrame().getLocation().getY(), ANNOTATION_DELIMITER,
				stripMetaCharacters(this.annotatedText), ANNOTATION_DELIMITER, 
				stripMetaCharacters(this.getUserText()));
		return line;
	}

	private String stripMetaCharacters(String input) {
		// prevent users from messing up input formatting when we load up the data again 
		// since we will interpret "\\n" as the newline character. normal users should 
		// never enter "\\n" anyway, so it should be a safe transformation. 
		while(input.contains("\\n")){
			input = input.replace("\\n", "");
		}
		input = input.replace(ANNOTATION_DELIMITER, ""); 
		return input.replace("\n", "\\n");
	}


	public Annotation(String docKey, int startIndex, int endIndex, Point position, String annotatedText, String userText){
		this.startIndex = startIndex;
		this.endIndex = endIndex;
		this.annotatedText = annotatedText;
		this.docKey = docKey;

		textArea = new JTextArea();
		textArea.setEditable(true);
		textArea.setLineWrap(true);
		textArea.setText(userText);
		textArea.setBorder(BorderFactory.createLineBorder(Color.black));
		textArea.setWrapStyleWord(true);

		frame = new JInternalFrame("", false, false, false, false); // title, resizable, closable, maximizable, iconifiable (minimizable)
		// Scroll
		JScrollPane scroll = new JScrollPane(textArea);
		frame.add(scroll);
//		frame.setMaximumSize(new Dimension(ANNOTATION_MARGIN, (int) frame.getSize().getHeight()));
		frame.setBounds(new Rectangle(0,0,ANNOTATION_MARGIN, (int) frame.getSize().getHeight()));
		frame.setLocation(position);
	}


	public JInternalFrame getFrame(){
		return frame;
	}

	public JTextArea getTextArea(){
		return textArea;
	}

	public String getUserText(){
		return textArea.getText();
	}
}
